package com.atguigu.jxc.controller;

import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/listInventory")
    public String listInventory(HttpServletRequest request){
        Integer page = Integer.valueOf(request.getParameter("page"))-1;
        Integer rows = Integer.valueOf(request.getParameter("rows"));
        String codeOrName = request.getParameter("codeOrName");
        String goodsTypeId = request.getParameter("goodsTypeId");
        System.out.println("goodsTypeId = " + goodsTypeId);
        Map<String,Object> map = goodsService.getList(page,rows,codeOrName);
        Gson gson = new Gson();
        String result = gson.toJson(map);
        return result;
    }

    @PostMapping("/list")
    public String getList(@RequestParam(value = "page",required = false)Integer page,
                          @RequestParam(value = "rows",required = false)Integer rows,
                          @RequestParam(value = "goodsName",required = false)String goodsName,
                          @RequestParam(value = "goodsTypeId",required = false)String goodsTypeId){
        System.out.println("goodsTypeId = " + goodsTypeId);
        Map<String,Object> map = goodsService.getList(page-1,rows,goodsName);
        Gson gson = new Gson();
        String result = gson.toJson(map);
        return result;
    }

    @PostMapping("/save")
    public String save(Goods goods,@RequestParam(required = false)String goodsId){
        return goodsService.save(goods,goodsId);
    }
}
